package com.qf.lzhspringbootswaggerstatter.conf;


import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@ConditionalOnProperty(prefix = "qfsy" ,name = "swagger")
@EnableConfigurationProperties(QfSyProperties.class)
@EnableSwagger2
@EnableKnife4j
public class QfAutoConfiguration {
    @Autowired
    QfSyProperties qfSyProperties;

    @Bean
    public Docket buildDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(buildApiInfo())
                .select()
                // 要扫描的API(Controller)基础包
                .apis(RequestHandlerSelectors.basePackage(qfSyProperties.getBaskPackage()))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo buildApiInfo() {
        Contact contact = new Contact(qfSyProperties.getContactName(),qfSyProperties.getContactURL(),qfSyProperties.getContactemail());
        return new ApiInfoBuilder()
                .title(qfSyProperties.getContactTittle())
                .description(qfSyProperties.getDescription())
                .contact(contact)
                .version(qfSyProperties.getVersion()).build();
    }

}
